<?php

class Training_AuthController extends Zendvn_Controller_Action {

    //Mang tham so nhan duoc o moi action
    protected $_arrParam;
    //Duong dan cua controller
    protected $_currentController;
    //Duong dan cua Action chinh
    protected $_actionMain;
    //Thong so phan trang
    protected $_paginator = array(
        'ItemCountPerPage' => 5,
        'PageRange' => 3,
    );
    protected $_namespace;

    public function init() {
        //Mang tham so nhan duoc o moi action
        $this->_arrParam = $this->_request->getParams();

        //Duong dan cua controller
        $this->_currentController = '/' . $this->_arrParam['module']
                . '/' . $this->_arrParam['controller'];

        //Duong dan cua Action chinh
        $this->_actionMain = $this->_currentController . '/index';

        $this->_paginator['currentPage'] = $this->_request->getParam('page', 1);
        $this->_arrParam['paginator'] = $this->_paginator;

        //Luu cac du lieu filter vao SESSION
        //Dat ten SESSION
        $this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
        $ssFilter = new Zend_Session_Namespace($this->_namespace);
//		$ssFilter->unsetAll();
        if (empty($ssFilter->col)) {
            $ssFilter->keywords = '';
            $ssFilter->col = 'g.id';
            $ssFilter->order = 'DESC';
        }

        $this->_arrParam['ssFilter']['keywords'] = $ssFilter->keywords;
        $this->_arrParam['ssFilter']['col'] = $ssFilter->col;
        $this->_arrParam['ssFilter']['order'] = $ssFilter->order;

        //Truyen ra view
        $this->view->arrParam = $this->_arrParam;
        $this->view->currentController = $this->_currentController;
        $this->view->actionMain = $this->_actionMain;

        $template_path = TEMPLATE_PATH . "/admin/system";
        $this->loadTemplate($template_path, 'template.ini', 'template');
    }

    public function indexAction() {
        
        //1. Gọi kết nối với Zend Db
        $db = Zend_Registry::get('connectDb');
        
        //2. Khởi tạo Zend Auth
        $auth = Zend_Auth::getInstance();
        
        //3. 
        $authAdapter = new Zend_Auth_Adapter_DbTable($db, $tableName, $identityColumn, $credentialColumn, $credentialTreatment);
        //$authAdapter = new Zend_Auth_Adapter_DbTable(
        //                              $zendDb, $tableName, $identityColumn, $credentialColumn, $credentialTreatment)
        $authAdapter->setTableName('users')
                    ->setIdentityColumn('user_name')
                    ->setCredentialColumn('password');
        $select = $authAdapter->getDbSelect();
        $select->where('status = 1')
               ->where(' active_code is NULL');
        
        if($this->_request->isPost()){

            $encode = New Zendvn_Encode();
            $user_name = $this->_arrParam['user_name'];
            $password  = $encode->password($this->_arrParam['password']);
            $authAdapter->setIdentity($user_name)
                        ->setCredential($password);
            
            //Lay kết quả truy vấn của Zend_Auth
            $result = $auth->authenticate($authAdapter);
            if(!$result->isValid()){
                $error = $result->getMessages();
                echo '<br/>' . current($error);
            }else{
                echo 'Đăng nhập thành công';
                //Lấy thông tin của tài khoản đưa vào session của Zend Auth
                $returnColumns = array('id', 'user_name', 'email');
                $omitColumns = array('email');
                $data = $authAdapter->getResultRowObject(null, $omitColumns);
                $auth->getStorage()->write($data);
//                $info = $auth->getIdentity();
//                echo '<br/>' . $info->user_name;
            }
        }
    }
    
    public function loginAction() {
        $auth = Zend_Auth::getInstance();
        if($auth->hasIdentity()){
            echo 'login rồi';
        }else{
            echo 'chưa login';
        }
        $this->_helper->viewRenderer->setNoRender();    
    }
    
    public function logoutAction() {
        $auth = Zend_Auth::getInstance();
        $auth->clearIdentity();
        $this->_redirect($this->_actionMain);
        $this->_helper->viewRenderer->setNoRender();
    }
}
